﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

using Buddy.BehaviorTree;
using Buddy.Common;
using Buddy.CommonBot;
using Buddy.Common.Plugins;
using Buddy.Swtor;
using Buddy.Swtor.Objects;

using WingIt.Dynamics;

using Action = Buddy.BehaviorTree.Action;

namespace Buddywing.Plugins
{
    public class UseItem : IPlugin
    {
        #region Implementation of IEquatable<IPlugin>

        public bool Equals(IPlugin other)
        {
            return other.Name == Name;
        }

        #endregion

        #region Implementation of IPlugin

        public string Author { get { return "Neo93"; } }

        public Version Version { get { return new Version(1, 0); } }

        public string Name { get { return "UseItems"; } }

        public string Description { get { return "Use Relicts and Biochem Items"; } }

        public Window DisplayWindow { get { return null; } }

        public int hpPlayer { get; set; }
        public int hpBoss { get; set; }
        public int hpNormal { get; set; }
        public int hpMedpac { get; set; }
        public int hp { get; set; }

        public TorItem[] UsableCombat = new TorItem[10];
        public TorItem Medpac { get; set; }
        public TorItem Stim { get; set; }

        private DateTime LastChecked { get; set; }

        /// <summary> Executes the pulse action. This is called every "tick" of the bot. </summary>
        public void OnPulse()
        {
            hpPlayer = 50;
            hpBoss = 15;
            hpNormal = 80;
            hpMedpac = 20;
            hp = 0;

            if (DateTime.Now.Subtract(LastChecked).TotalSeconds > 1)
            {
                LastChecked = DateTime.Now;

                if (Medpac != null && BuddyTor.Me.HealthPercent <= hpMedpac)
                {
                    Medpac.Use();
                }

                if (Stim != null && !BuddyTor.Me.HasBuff(Stim.Name))
                {
                    Stim.Use();
                }

                if (UsableCombat != null && BuddyTor.Me.InCombat)
                {
                    foreach (var i in UsableCombat)
                    {
                        if (i != null)
                        {
                            switch (BuddyTor.Me.CurrentTarget.Toughness)
                            {
                                case CombatToughness.Standard:
                                    hp = hpNormal;
                                    break;
                                case CombatToughness.Strong:
                                case CombatToughness.Player:
                                    hp = hpPlayer;
                                    break;
                                case CombatToughness.RaidBoss:
                                    hp = hpBoss;
                                    break;

                                default:
                                    break;
                            }
                            if (BuddyTor.Me.CurrentTarget.HealthPercent >= hp)
                            {
                                i.Use();
                            }
                        }
                    }
                }
            }
        }

        /// <summary> Executes the initialize action. This is called at initial bot startup. (When the bot itself is started, not when Start() is called) </summary>
        public void OnInitialize()
        {
            int numb = 0;
            int med = 0;
            int stim = 0;

            foreach (var r in BuddyTor.Me.InventoryEquipped.Where(item => item.Name.Contains("Relic")))
            {
                UsableCombat[numb] = r;
                numb++;
            }

            foreach (var i in BuddyTor.Me.InventoryEquipment.Where(item => item.Name.Contains("Rakata") && item.Name.Contains("Adrenal")))
            {
                UsableCombat[numb] = i;
                numb++;
            }
            foreach (var m in BuddyTor.Me.InventoryEquipment.Where(item => item.Name == "Rakata Medpac"))
            {
                Medpac = m;
                med++;
            }
            foreach (var s in BuddyTor.Me.InventoryEquipment.Where(item => item.Name.Contains("Stim") && (item.Name.Contains("Rakata") || item.Name.Contains("Reusable"))))
            {
                Stim = s;
                stim++;
            }
        }

        /// <summary> Executes the shutdown action. This is called when the bot is shutting down. (Not when Stop() is called) </summary>
        public void OnShutdown()
        {
        }

        /// <summary> Executes the enabled action. This is called when the user has enabled this specific plugin via the GUI. </summary>
        public void OnEnabled()
        {
            Logging.Write("[UseItem] Enabled");
        }

        /// <summary> Executes the disabled action. This is called whent he user has disabled this specific plugin via the GUI. </summary>
        public void OnDisabled()
        {
            Logging.Write("[UseItem] Disabled");
        }

        #endregion
    }
}
